aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/send/invoice/[id]/+page.server.js
blob: 0d23d3070fb9876cb9280ceaada3aaf4fb85ed8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { error, fail, redirect } from '@sveltejs/kit';
import { fd, auth, get, post } from '$lib/utils';

export async function load({ params: { id }, parent, url }) {
	let { user } = await parent();

	let { amount, address, rate, prompt, tip, text, user: recipient } = await get(`/invoice/${id}`);

  if (prompt && tip === null) throw redirect(307, `/${recipient.username}/invoice/${id}/tip`);
	if (recipient.username === user.username) throw error(500, { message: 'Cannot send to self' });
	return { amount, address, tip, rate, payreq: text, recipient };
}

export const actions = {
	default: async ({ cookies, params: { hash }, request }) => {
		try {
			let body = await fd(request);
			body.hash = hash;

			await post('/payments', body, auth(cookies));
		} catch (e) {
			return fail(400, { message: e.message });
		}

		throw redirect(307, '/sent');
	}
};